package com.hybrid.urlintercept;

import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import com.hybrid.urlintercept.WVURLInterceptData.RuleData;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-09 22:36
 * @version: 9.1.0
 */
public class WVURLInterceptService {
    private static Set<RuleData> urlRules = Collections.synchronizedSet(new HashSet<RuleData>());
    private static Map<String, Pattern> rulePat = Collections.synchronizedMap(new HashMap<String, Pattern>());

    private static WVURLIntercepterInterface mIntercepter = null;

    private static WVURLIntercepterHandler mHandler = null;

    public static Set<RuleData> getWVURLinterceptRules() {
        return urlRules;
    }

    public static void registerWVURLinterceptRules(Set<RuleData> urlRules) {
        //进行 decode
        for (Iterator<RuleData> it = urlRules.iterator(); it !=null && it.hasNext();) {
            RuleData data = it.next();
            if(data.needdecode) {
                try {
                    data.pattern = java.net.URLDecoder.decode(data.pattern,"utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }
        WVURLInterceptService.urlRules = urlRules;
    }

    public static Map<String, Pattern> getWVURLInterceptRulePats() {
        return rulePat;
    }

    public static void registerWVURLInterceptRulePats(Map<String, Pattern> rulePat) {
        WVURLInterceptService.rulePat = rulePat;
    }

    public static WVURLIntercepterInterface getWVURLIntercepter() {
        return mIntercepter;
    }

    public static void registerWVURLIntercepter(WVURLIntercepterInterface mIntercepter) {
        WVURLInterceptService.mIntercepter = mIntercepter;
    }

    public static void resetRulesAndPat() {
        urlRules.clear();
        rulePat.clear();
    }

    public static WVURLIntercepterHandler getWVURLInterceptHandler() {
        return mHandler;
    }

    public static void registerWVURLInterceptHandler(WVURLIntercepterHandler mHandler) {
        WVURLInterceptService.mHandler = mHandler;
    }
}
